package xdu.lz.stage1_basic.chapter2_api.d_join_interrupt_wait;

public class Code04_Join {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        Thread.sleep(10L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" ->"+i);
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        Thread.sleep(10L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" ->"+i);
                }
            }
        });
        t1.start();
        t2.start();
        //谁执行了main，谁就要等待当前join的线程结束后再执行自己。
        //这里是main执行了join，那么只有t1结束后 ，main线程才继续执行。
        t1.join();

        t2.join();
        System.out.println(Thread.currentThread().getName()+" finished");
    }
}
